<template>
  <div class="loadingImg">
    <div class="img_box">
      <img src="../../assets/images/loading_2.gif" alt="">
      <div class="loadingText">正在获取登陆的用户信息，请稍后。。。</div>
    </div>
    <!-- <div>加载中。。。</div> -->
  </div>
</template>

<script>
import Oidc from 'oidc-client';
// import { decrypt } from '@/lib/cache';
import { setToken, setTokenType, setOidcUser, getTenantId, setTenantId } from '@/lib/auth';

export default {
  mounted() {
    new Oidc.UserManager({ response_mode: 'query' })
      .signinRedirectCallback()
      .then(
        user => {
          console.log(user);
          const token = user ? user.access_token : getToken();
          const token_type = user ? user.token_type : getLocalTokentype() ? getLocalTokentype() : 'Bearer';
          setToken(token);
          setTokenType(token_type);
          setOidcUser(user);
          let urls = user && user.state && typeof user.state === 'string' ? window.atob(user.state) : 'index.html';
          // tenantid检测
          const _tenantId = getTenantId();
          if (_tenantId !== user.profile.tenantid) {
            setTenantId(user.profile.tenantid);
          }
          // 处理地址携带tenantid
          const _t = window.atob(user.state).match(/tenantid=[a-z0-9-]*(&|$)/);
          if (_t && _t.length > 0) {
            const _ts = _t[0].replace('tenantid=', '').replace('&', '');
            if (_ts !== user.profile.tenantid) {
              urls = '/game.html#/myCenter';
            }
          }
          if (navigator.userAgent.match(/(phone|iPhone|ios|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
            this.envir = 'mobile';
            // 手机端  如果是pc端赛事地址  自动导向手机端赛事地址   如果是后台地址或者其他系统地址不允许登陆
            let _urls = urls;
            if (urls.indexOf('/game.html') > -1) {
              _urls = _urls.replace('/game.html', '/mobile.html').split('/').splice(0, 7).join('/');
            } else if (urls.indexOf('/mobile.html') === -1 && urls.indexOf('/mbleague.html') === -1) {
              window.history.forward(1);
              this.$message.error('该登录地址不支持手机端登陆，请使用其他登陆地址！！！');
              this.$router.push({
                path: '/404',
                query: this.$route.query
              });
              return false;
            }

            window.location.replace(_urls);
          } else {
            window.location.replace(urls);
          }
        },
        error => {
          console.error(error);
          const _href = WEB_CONFIG.VUE_APP_API_CERTIFICAT.split('/').splice(0, 3).join('/') + '/game.html#/myCenter';
          window.location.replace(_href);
          // const query = this.$route.query;
          // query.beforUrl = encodeURIComponent(window.location.href);
          // window.history.forward(1);
          // this.$router.push({
          //   path: '/user',
          //   query: query
          // });
          // window.location.href = window.location.origin + window.location.pathname + '#/user';
        }
      );
  }
};
</script>

<style lang="scss">
.loadingImg {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-flow: column;
  height: 100vh;
  margin: 0 auto;
}
.img_box{
  position: relative;
  width: 41%;
}
.loadingImg img {
  width: 100%;
}
.loadingText{
  position: absolute;
  bottom: 90px;
  color: #1890ff;
  font-size: 20px;
  font-weight: bolder;
  text-align: center;
  width: 100%;
  text-shadow: 0px 2px 10px #ff5a5a;
}
</style>
